import java.util.PriorityQueue;

public class MinOperations {

    public static void main(String[] args) {
        MinOperations minOperations = new MinOperations();
        int[] nums = {2,3,2,4,3};
        int[] temp = {9,6,9,3,11};
        System.out.println(minOperations.minOperations(nums, temp));
    }

    public int minOperations1(int[] nums, int[] numsDivide) {
        int gcd = gcd(numsDivide[0], numsDivide.length == 1 ? numsDivide[0] : numsDivide[1]);
        int last = gcd;
        for (int i = 2; i < numsDivide.length; i++) {
            int temp = gcd(numsDivide[i - 1], numsDivide[i]);
            int temp2 = gcd(temp, last);
            if (temp2 == 1) {
                int temp3 = temp * last;
                if (Math.min(numsDivide[i], Math.min(numsDivide[i - 1], numsDivide[i - 2])) < temp3 || last == 1 || temp == 1) {
                    gcd = 1;
                    break;
                } else gcd = temp3;
            } else {
                gcd = Math.min(gcd, temp2);
            }
            last = temp;
        }
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        for (int i = 0; i < nums.length; i++) {
            heap.add(nums[i]);
        }
        while (!heap.isEmpty() && gcd % heap.peek() != 0) heap.poll();
        return heap.isEmpty() ? -1 : nums.length - heap.size();
    }


    public int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }

    public int minOperations(int[] nums, int[] numsDivide) {
        int gcd = numsDivide[0];
        for (int i = 1; i < numsDivide.length; i++) {
            gcd = gcd(gcd, numsDivide[i]);
        }
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        for (int i = 0; i < nums.length; i++) {
            heap.add(nums[i]);
        }
        while (!heap.isEmpty() && gcd % heap.peek() != 0) heap.poll();
        return heap.isEmpty() ? -1 : nums.length - heap.size();
    }
}